<template>
<d2-container>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
           style="width: 30%;">
    <el-form-item label="名字" prop="alias">
      <el-input v-model="ruleForm.alias"></el-input>
    </el-form-item>
<!--    <el-form-item label="供应商" prop="supplier">-->
<!--      <el-input type="supplier" v-model="ruleForm.supplier" autocomplete="off"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="供应商" prop="supplier">
      <el-select v-model="ruleForm.supplier" placeholder="请选择供应商">
        <el-option v-for="item in ruleForm.supplier_category" :label="item" :value="item"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="总量" prop="total">
      <el-input type="total" v-model="ruleForm.total" autocomplete="off"  placeholder="请填写原料总量"></el-input>
    </el-form-item>
    <el-form-item label="剩余" prop="surplus" >
      <el-input type="text" v-model="ruleForm.surplus"  placeholder="请填写剩余原料数量"></el-input>
    </el-form-item>
    <el-form-item label="到期时间" prop="deadline">
      <el-col :span="15">
        <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.deadline"
                        style="width: 100%;"></el-date-picker>
      </el-col>
    </el-form-item>

    <el-form-item label="类型" prop="type">
      <el-select v-model="ruleForm.type" placeholder="请选择原材料类型">
        <el-option v-for="item in ruleForm.type_category" :label="item" :value="item"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="备注" prop="remark">
      <el-input v-model="ruleForm.remark"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>

</d2-container>
</template>

<script>
const axios = require('axios');
import {request} from "@/network/request";

export default {
  name: 'create',
  methods: {
    submitForm(formName) {
      let _this = this;
      request({
          url: 'material/add',
          method: 'post',
          params: {
            alias: _this.ruleForm.alias + "---" + new Date().toLocaleDateString(),
            supplier: _this.ruleForm.supplier,
            surplus: _this.ruleForm.surplus,
            total: _this.ruleForm.total,
            deadline: _this.ruleForm.deadline,
            type: _this.ruleForm.type,
            remark: _this.ruleForm.remark
          }
        }, res => {

          alert("添加成功！");
          console.log(res);
        }, err => {
          alert("添加失败，请重试！");
          console.log(err);
        }
      );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    let _this = this;
    //原材料分类
    request({
      url: 'materialCategory/query',
      params: {
        key: '',
        pages: '1',
        num: 100
      },
    },
      res => {
      console.log(res.data.records.length);
      _this.ruleForm.type_category = [];
      for (let i = 0; i < res.data.records.length; i++) {
        _this.ruleForm.type_category.push(res.data.records[i].name) ;
      }
    },
      err => {
      console.log(err);
    });
    //供应商
      request({
          url: 'supplier/query',
          params: {
            key: '',
            pages: '1',
            num:100
          },
        },
        res => {
          _this.ruleForm.supplier_category = [];
          for (let i = 0; i < res.data.records.length; i++) {
            _this.ruleForm.supplier_category.push(res.data.records[i].fullName) ;
          }
        },
        err => {
          console.log(err);
        })
  },
  data() {
    return {
      ruleForm: {
        alias: null,
        supplier: null,
        total: null,
        surplus: null,
        deadline: null,
        type_category:null,
        supplier_category:null,
        type:null,
        remark: '无'
      },
      rules: {
        alias: [
          {required: true, message: '请输入原料名称', trigger: 'blur'}
        ],
        supplier: [
          {required: true, message: '请输入供应商', trigger: 'blur'}
        ],
        total: [
          {required: true, message: '请输入总量', trigger: 'blur'}
        ],
        surplus: [
          {required: true, message: '请输入剩余数量', trigger: 'blur'}
        ],
        deadline: [
          {required: true, message: '请输入到期时间', trigger: 'blur'}
        ]
      }
    };
  }
}
</script>
